/**
 * 获取单个元素的信息 
 * ctx 页面的this实例
 * selector选择器
 */
export const getElementInfo = (ctx, selector) => {
	return new Promise((resolve, reject) => {
		const query = uni.createSelectorQuery().in(ctx)
		query
		  .select(selector)
		  .boundingClientRect((data) => {
		    resolve(data)
		  })
		  .exec();
	})
}

/**
 * 获取多个元素的信息 
 * ctx 页面的this实例
 * selector选择器
 */
export const getElementsInfo = (ctx, selector) => {
	return new Promise((resolve, reject) => {
		const query = uni.createSelectorQuery().in(ctx)
		query
		  .selectAll(selector)
		  .boundingClientRect((data) => {
		    resolve(data)
		  })
		  .exec();
	})
}

/**
 * 防抖函数
 */
export const debounce = (fn, time = 300) => {
	let timer = null
	return function (...argus) {
		clearTimeout(timer)
		timer = setTimeout(() => {
			fn && fn.apply(this, argus)
		}, time)
	}
}


/**
 * 格式化时间
 */
export const timeTransform = (v, unit = 'second', type="HH:mm:ss") => {
	if (unit === 'minute') {
		v = v * 60
	} else if (unit === 'hour') {
		v = v * 3600
	}
	
	const hours = Math.floor(v / 3600) + ''
	const minutes = Math.floor((v - 3600 * hours) / 60) + ''
	const seconds = v - 3600 * hours - 60 * minutes + ''
	return type.replace(/hh/ig, hours.padStart(2, '0'))
		.replace(/mm/ig, minutes.padStart(2, '0'))
		.replace(/ss/ig, seconds.padStart(2, '0'))
}